package utils;

import entry.Library;
import entry.Goods;
import org.apdplat.word.WordSegmenter;
import org.apdplat.word.segmentation.WordRefiner;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class ParticipleUtils {

    //批量分词
    public Library batchParticipleEachCate(List<Goods> goodsList) {
        Library library = new Library();
        library.setCategory(goodsList.get(0).getType());
        for (Goods goods : goodsList){
            String goods_name = goods.getName();
            //分词
            //String word = WordSegmenter.seg(goods_name).toString();
            String word = WordRefiner.refine(WordSegmenter.seg(goods_name)).toString();
            //生成词库
            generateLib(library,word);
        }
        //System.out.println(goodsList.iterator().next().getType());
        return library;
    }

    //批量分组分词
    public List<Library> batchParticiple(List<List<Goods>> goodsList){
        Iterator iterator = goodsList.iterator();
        List<Library> result = new ArrayList<Library>();
        while (iterator.hasNext()){
            result.add(batchParticipleEachCate((List<Goods>) iterator.next()));
        }
        return result;
    }

    //单个分词(测试用)
    public void Participle(Goods goods) {
        String goods_name = goods.getName();
        System.out.println(goods_name);
        System.out.println(WordRefiner.refine(WordSegmenter.seg(goods_name)));
        System.out.println();
    }

    //生成词汇库
    public void generateLib(Library library,String word){
        String[] words = word.substring(1,word.length()-1).split(",");
        for (String s:words){
            library.put(s.trim());
        }
    }
}
